package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Date;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;

import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;

import negocio.Alumno;
import negocio.Tarea;
import negocio.TareaControlador;
import negocio.Usuario;
import negocio.UsuarioControlador;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TareasEditar extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	Tarea tarea;
	JComboBox comboBox;
	ArrayList<Usuario> listaUsuarios=new ArrayList<Usuario>();
	
	
	
	public void llenarComboUsuarios()
	{
		ArrayList<Usuario> aux = UsuarioControlador.getAll();
		for(Usuario u:aux)
		{
			
			if(u.getPerfil().equals("Administrativo") || u.getPerfil().equals("supervisor"))
			{
				listaUsuarios.add(u);
				comboBox.addItem(u);
			}
			
		}
		
	}
	

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TareasCrear frame = new TareasCrear();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TareasEditar(Tarea tarea, Tareas ventanaTareas) {
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				ventanaTareas.nullTareasEditar();
				dispose();
			}
		});
		
		this.tarea=tarea;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setTitle("Examinar tarea");
		setBounds(100, 100, 349, 309);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblTipoDeTarea = new JLabel("Tipo de tarea");
		lblTipoDeTarea.setBounds(10, 11, 80, 14);
		contentPane.add(lblTipoDeTarea);
		
		JLabel lblAlumno = new JLabel("DNI alumno");
		lblAlumno.setBounds(10, 39, 80, 14);
		contentPane.add(lblAlumno);
		
		JLabel lblUsuarioAsignado = new JLabel("Usuario Asignado");
		lblUsuarioAsignado.setBounds(10, 95, 104, 14);
		contentPane.add(lblUsuarioAsignado);
		
		JLabel lblFechaLimite = new JLabel("Fecha limite");
		lblFechaLimite.setBounds(10, 64, 80, 14);
		contentPane.add(lblFechaLimite);
		
		textField = new JTextField();
		textField.setEnabled(false);
		textField.setEditable(false);
		textField.setBounds(124, 33, 157, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		textField.setText(tarea.getAlumno().getDni());
		
		JDateChooser dateChooser = new JDateChooser();
		dateChooser.setBounds(124, 61, 157, 20);
		contentPane.add(dateChooser);
		dateChooser.setDate(tarea.getFechaLimite());
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Comentarios de la tarea", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(4, 118, 326, 110);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JTextPane textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setBounds(6, 16, 314, 87);
		panel.add(textPane);
		textPane.setText(tarea.getComentarios());
		
		JButton btnCancelar = new JButton("Salir");
		btnCancelar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ventanaTareas.nullTareasEditar();
				dispose();
			}
		});
		btnCancelar.setBounds(234, 239, 89, 23);
		contentPane.add(btnCancelar);
		
		textField_1 = new JTextField();
		textField_1.setEnabled(false);
		textField_1.setEditable(false);
		textField_1.setBounds(124, 8, 157, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		textField_1.setText(tarea.getTipoTarea());
		
		JButton btnEditar = new JButton("Editar");
		btnEditar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//int id, Date fechaLimite, String tipoTarea, Usuario usuarioCreador, Usuario usuarioAsignado, String estado, Alumno alumno, String comentarios)
				java.sql.Date sqldate = new java.sql.Date(dateChooser.getDate().getTime());
				Tarea tareaAux = new Tarea(tarea.getId(), sqldate, tarea.getTipoTarea(), tarea.getUsuarioCreador(), (Usuario)comboBox.getSelectedItem(),tarea.getEstado(),tarea.getAlumno(), tarea.getComentarios(),tarea.getContactoApertura(), null);
				TareaControlador.actualizar(tareaAux);
				ventanaTareas.llenarTabla();
				dispose();
			}
		});
		btnEditar.setBounds(135, 239, 89, 23);
		contentPane.add(btnEditar);
		
		comboBox = new JComboBox();
		comboBox.setBounds(124, 92, 157, 20);
		contentPane.add(comboBox);
		llenarComboUsuarios();
		comboBox.setSelectedItem(comboBox.getItemAt(listaUsuarios.indexOf(tarea.getUsuarioAsignado())));
	}
}
